<?php 
	namespace Home\Controller;

	class ListController extends HomeController
	{
		
		public function index(){
			// 查询分类表
			$sort = M('list');

			$list = $sort->where('pid = 0')->select();
			$array = $sort->select();

			

			$showapi_appid = '27066'; 
		    $showapi_secret = '9e0f60470f5b4078bf41b118e7da7a7c'; 
		    $paramArr = array(
		         'showapi_appid'=> $showapi_appid,
		         'keyword'=> "",
		         'typeId'=> I('type',null),
		         'pre_match'=> "",
		         'page'=> I('id',0)
		         //添加其他参数
		    	);

		// echo I('type',null);
		    // var_dump();

		    $param = $this->createParam($paramArr,$showapi_secret);
		    $url = 'http://route.showapi.com/211-2?'.$param; 
		    // 获取小说内容
		    $result = file_get_contents($url);
		    // 对内容进行解码解码
		    $jsonObj = json_decode($result);

		    $book = $jsonObj->showapi_res_body->pagebean->contentlist;
		   	$aa = array();
		    foreach($book as $vo){
		    	$a = explode(" ",$vo->name);
				$aa[] = array($a['0'],'id'=>$vo->id,'author'=>$vo->author,'typeName'=>$vo->typeName,'type'=>$vo->type);

		    }
		    // echo "<pre>";
		    // print_r($aa);

			$this->assign('list',$list);
			$this->assign('pid',$array);

			$this->assign('book',$aa);

			
			$this->display();
		}

		 //创建参数(包括签名的处理)
	    public function createParam ($paramArr,$showapi_secret) {
	         $paraStr = "";
	         $signStr = "";
	         ksort($paramArr);
	         foreach ($paramArr as $key => $val) {
	             if ($key != '' && $val != '') {
	                 $signStr .= $key.$val;
	                 $paraStr .= $key.'='.urlencode($val).'&';
	             }
	         }
	         $signStr .= $showapi_secret;//排好序的参数加上secret,进行md5
	         $sign = strtolower(md5($signStr));
	         $paraStr .= 'showapi_sign='.$sign;//将md5后的值作为参数,便于服务器的效验

	         return $paraStr;
	    }
	}